<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="index">
      <span v-if="index === breadcrumbs.length - 1" class="no-redirect">{{ item.meta.title }}</span>
      <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();
const breadcrumbs = ref([]);

// 处理路由变化，更新面包屑
const getBreadcrumbs = () => {
  const matched = route.matched.filter(item => item.meta && item.meta.title);
  
  // 如果第一个不是首页，添加首页为第一个面包屑
  if (matched.length > 0 && matched[0].path !== '/device-owner/dashboard') {
    matched.unshift({
      path: '/device-owner/dashboard',
      meta: { title: '首页' }
    });
  }
  
  breadcrumbs.value = matched;
};

// 处理面包屑链接点击
const handleLink = (item) => {
  router.push(item.path);
};

// 监听路由变化
watch(
  () => route.path,
  () => getBreadcrumbs(),
  { immediate: true }
);
</script>

<style scoped>
.el-breadcrumb {
  font-size: 14px;
  line-height: 1.5;
}

.no-redirect {
  color: #97a8be;
  cursor: text;
}
</style> 